﻿using System;
using System.Data.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVVM.Northwind.Employee.DataAccess;
using MVVM.Northwind.Employee.Utilities;

namespace MVVM.Northwind.Employee.Model
{
    public class EmployeeRepository
    {
        private List<DataAccess.Employee> _employeeList ;

        public EmployeeRepository()
        {
            _employeeList= new List<DataAccess.Employee>();
            GetEmployeesFromDataBase();
        }

        public DataAccess.Employee GetEmployee(int EmployeeID)
        {
            try
            {
                if (_employeeList != null)
                {
                    DataAccess.Employee emp = _employeeList.Find((e) => { return e.EmployeeID == EmployeeID; });
                    return emp;
                }
                else
                    return null;
                
            }
            catch (Exception ex)
            {
                ErrorHandler.ErrorMessage = ex.Message;
                throw;
            }
        }


        private void GetEmployeesFromDataBase()
        {
            try
            {
                DatabaseGateway dbGwy = DatabaseGateway.CreateInstance();
                List<DataAccess.Employee> employees = dbGwy.GetEmployees();

                foreach (var item in employees)
                {
                    _employeeList.Add(item);
                }
                
            }
            catch (Exception ex)
            {
                ErrorHandler.ErrorMessage = ex.Message;
                throw;
            }
        }

        public List<DataAccess.Employee> SearchEmployees(string name)
        {
            try
            {
                DatabaseGateway dbGwy = DatabaseGateway.CreateInstance();
                List<DataAccess.Employee> employees= dbGwy.SearchEmployees(name);
                List<DataAccess.Employee> empList = new List<DataAccess.Employee>();
                foreach (var item in employees)
                {
                    empList.Add(item);
                }
                return empList;
            }
            catch (Exception ex)
            {
                ErrorHandler.ErrorMessage = ex.Message;
                throw;
            }
        }

        
        //private DataAccess.Employee GenerateEmployee(DataAccess.Employee employee)
        //{          
        //    DataAccess.Employee emp = new DataAccess.Employee();
        //    emp.Address = employee.Address;
        //    emp.BirthDate = employee.BirthDate;
        //    emp.City = employee.City;
        //    emp.Country = employee.Country;
        //    emp.EmployeeID = employee.EmployeeID;
        //    emp.Extension = employee.Extension;
        //    emp.FirstName = employee.FirstName;
        //    emp.HireDate = employee.HireDate;
        //    emp.HomePhone = employee.HomePhone;
        //    emp.LastName = employee.LastName;
        //    emp.Notes = employee.Notes;
        //    emp.Photo = employee.Photo;
        //    emp.PostalCode = employee.PostalCode;
        //    emp.Region = employee.Region;
        //    emp.Title = employee.Title;
        //    return emp;
        //}
        public List<DataAccess.Employee> GetEmployees()
        {
            if (_employeeList==null)
            {
                _employeeList = new List<DataAccess.Employee>();
            }
            return _employeeList;
        }
    }
}
